How to query and transform XML data into HTML using XQuery in Stylus Studio X15 (Part 2)

Blessings Photo

Blessings
7 years 1 Views
Category:
Description:

In this video I will be showing you how to query and transform XML data into HTML using XQuery. The source code can be ...

To query and transform XML data into HTML using XQuery in Stylus Studio, you can follow these steps:

Step 1: Open Stylus Studio

  1. Launch Stylus Studio.
  2. Create a new XQuery file.

Step 2: Load Your XML Data

Ensure you have your XML data available. For example, consider the following XML:

xml
<books>
  <book>
    <title>Learning XQuery</title>
    <author>John Doe</author>
    <price>29.99</price>
  </book>
  <book>
    <title>Advanced XQuery</title>
    <author>Jane Smith</author>
    <price>39.99</price>
  </book>
</books>

Step 3: Write the XQuery

You can write a query to transform the XML data into HTML. Here's an example of how to do this:

xquery
xquery version "1.0";

declare namespace ex = "http://example.com";

let $xml := 
<books>
  <book>
    <title>Learning XQuery</title>
    <author>John Doe</author>
    <price>29.99</price>
  </book>
  <book>
    <title>Advanced XQuery</title>
    <author>Jane Smith</author>
    <price>39.99</price>
  </book>
</books>

return
<html>
  <head>
    <title>Book List</title>
  </head>
  <body>
    <h1>Available Books</h1>
    <ul>
      for $book in $xml/book
      return
        <li>
          <strong>{$book/title/text()}</strong> by {$book/author/text()} - 
          <em>{$book/price/text()}</em>
        </li>
    </ul>
  </body>
</html>

Step 4: Execute the Query

  1. Save your XQuery file with a .xqy extension.
  2. Run the query in Stylus Studio.

Step 5: View the Output

The output will be an HTML document structured like this:

html
<html>
  <head>
    <title>Book List</title>
  </head>
  <body>
    <h1>Available Books</h1>
    <ul>
      <li><strong>Learning XQuery</strong> by John Doe - <em>29.99</em></li>
      <li><strong>Advanced XQuery</strong> by Jane Smith - <em>39.99</em></li>
    </ul>
  </body>
</html>

html 1

Summary

This process allows you to convert XML data into an HTML format using XQuery in Stylus Studio. You can customize the HTML output further by modifying the XQuery code as needed. If you have specific requirements or need further assistance, feel free to ask!